#include "Common.h"
#include "World.h"
#include "Player.h"
#include "Opcodes.h"
#include "Chat.h"
#include "ObjectAccessor.h"
#include "Language.h"
#include "AccountMgr.h"
#include "SystemConfig.h"
#include "revision.h"
#include "Util.h"
#include "ScriptPCH.h"
#include <cstring>
#include "CharacterDatabaseCleaner.h"
#include "DatabaseEnv.h"
#include "ObjectMgr.h"
#include "Unit.h"
#include "Player.h"

class changer : public CreatureScript
{
	public:

    	changer()
        	: CreatureScript("changer")
    	{
    	}

    	void Options(Player* pPlayer, Creature* pCreature, int type , int count, int cost)
    	{
        	QueryResult resulta;
   		 QueryResult resultb;
   		 QueryResult resultc;
   		 QueryResult resultd;
   		 
        	resulta = LoginDatabase.PQuery("SELECT faction FROM account WHERE id = '%u'", pPlayer->GetSession()->GetAccountId());
        	char str_fac[200];
   		 
   		 resultb = LoginDatabase.PQuery("SELECT customize FROM account WHERE id = '%u'", pPlayer->GetSession()->GetAccountId());
        	char str_custom[200];
   		 
   		 resultc = LoginDatabase.PQuery("SELECT race FROM account WHERE id = '%u'", pPlayer->GetSession()->GetAccountId());
   		 char str_race[200];
   		 
   		 resultd = CharacterDatabase.PQuery("SELECT at_login FROM characters WHERE guid = '%u'", pPlayer->GetGUIDLow());
   		 char str_atlogin[200];
   		 
        	Field *fielda = resulta->Fetch();
        	uint32 faction = fielda[0].GetUInt32();
   		 
   		 Field *fieldb = resultb->Fetch();
        	uint32 custom = fieldb[0].GetUInt32();
   		 
   		 Field *fieldc = resultc->Fetch();
        	uint32 race = fieldc[0].GetUInt32();
   		 
   		 Field *fieldd = resultd->Fetch();
        	uint32 atlogin = fieldd[0].GetUInt32();   		 
   		 
        	if (!resulta || !resultb || !resultc || !resultd) 
        	{
            	sprintf(str_fac,"Ein Fehler ist aufgetreten. Bitte wende dich an einen Administrator!");
            	pPlayer->PlayerTalkClass->ClearMenus();
            	OnGossipHello(pPlayer, pCreature);
            	pCreature->MonsterSay(str_fac, LANG_UNIVERSAL, pPlayer->GetGUID());
            	return;
        	}   		 
   		 
   		 else if (type == 1)
        	{
   		   if (faction < cost)
            	{
             	sprintf(str_fac, "Du kannst zurzeit keine Fraktionswechsel machen!");
             	pPlayer->MonsterWhisper(str_fac,pPlayer->GetGUID(),true);
            	}   		 

   			else if(atlogin == 0)
   		 	{
             	LoginDatabase.PQuery("UPDATE account SET faction = faction - '%u' WHERE id = '%u'", cost, pPlayer->GetSession()->GetAccountId());
             	CharacterDatabase.PExecute("UPDATE characters SET at_login = '8' WHERE guid = '%u'", pPlayer->GetGUIDLow());
             	pPlayer->SetAtLoginFlag(AT_LOGIN_CHANGE_FACTION);

             	sprintf(str_fac,"Fraktionswechsel eingeleitet! Bitte ausloggen!");
             	pPlayer->MonsterWhisper(str_fac,pPlayer->GetGUID(),true);
   			 }
   			 
   			else
   		 	{
   			  sprintf(str_fac,"Auf diesem Charakter ist ein Fraktions/Rassenwechsel oder etwas Vergleichbares aktiv!!");
   			  pPlayer->MonsterWhisper(str_fac,pPlayer->GetGUID(),true);
   			 }
        	}
   		 
   		 else if (type == 2)
        	{
   		   if (custom < cost)
            	{
             	sprintf(str_custom, "Du kannst zurzeit keine Rundumanpassung machen!");
             	pPlayer->MonsterWhisper(str_custom,pPlayer->GetGUID(),true);
            	}   		 

   			else if(atlogin == 0)
   		 	{
             	LoginDatabase.PQuery("UPDATE account SET customize = customize - '%u' WHERE id = '%u'", cost, pPlayer->GetSession()->GetAccountId());
             	CharacterDatabase.PExecute("UPDATE characters SET at_login = '8' WHERE guid = '%u'", pPlayer->GetGUIDLow());
             	pPlayer->SetAtLoginFlag(AT_LOGIN_CUSTOMIZE);

             	sprintf(str_custom,"Rundumanpassung eingeleitet! Bitte ausloggen!");
             	pPlayer->MonsterWhisper(str_custom,pPlayer->GetGUID(),true);
   			 }
   			 
   			else
   		 	{
   			  sprintf(str_custom,"Auf diesem Charakter ist ein Fraktions/Rassenwechsel oder etwas Vergleichbares aktiv!!");
   			  pPlayer->MonsterWhisper(str_custom,pPlayer->GetGUID(),true);
   			 }
        	}
   		 
   		 else if (type == 3)
        	{
   		   if (race < cost)
            	{
             	sprintf(str_race, "Du kannst zurzeit keine Rassenwechsel machen!");
             	pPlayer->MonsterWhisper(str_race,pPlayer->GetGUID(),true);
            	}   		 

   			else if(atlogin == 0)
   		 	{
             	LoginDatabase.PQuery("UPDATE account SET race = race - '%u' WHERE id = '%u'", cost, pPlayer->GetSession()->GetAccountId());
             	CharacterDatabase.PExecute("UPDATE characters SET at_login = '8' WHERE guid = '%u'", pPlayer->GetGUIDLow());
             	pPlayer->SetAtLoginFlag(AT_LOGIN_CHANGE_RACE);

             	sprintf(str_race,"Rassenwechsel eingeleitet! Bitte ausloggen!");
             	pPlayer->MonsterWhisper(str_race,pPlayer->GetGUID(),true);
   			 }
   			 
   			else
   		 	{
   			  sprintf(str_race,"Auf diesem Charakter ist ein Fraktions/Rassenwechsel oder etwas Vergleichbares aktiv!!");
   			  pPlayer->MonsterWhisper(str_race,pPlayer->GetGUID(),true);
   			 }
        	}

        	if (type == 10)
        	{
            	sprintf(str_fac,"Noch %u Fraktionswechsel moeglich", faction);
            	pPlayer->MonsterWhisper(str_fac,pPlayer->GetGUID(),true);
        	}
   		 
        	if (type == 11)
        	{
            	sprintf(str_custom,"Noch %u Runumanpassung(en) moeglich", custom);
            	pPlayer->MonsterWhisper(str_custom,pPlayer->GetGUID(),true);
        	}
   		 
        	if (type == 12)
        	{
            	sprintf(str_race,"Noch %u Rassenwechsel moeglich", race);
            	pPlayer->MonsterWhisper(str_race,pPlayer->GetGUID(),true);
        	}

        	pPlayer->PlayerTalkClass->ClearMenus();
        	OnGossipHello(pPlayer, pCreature);
    	}

    	bool OnGossipHello(Player* pPlayer, Creature* pCreature)
    	{
        	pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Wieviele Fraktionswechsel stehen mir zur Verfuegung?", GOSSIP_SENDER_MAIN, 1000);
   		 pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Wieviele Rundumanpassungen stehen mir zur Verfuegung?", GOSSIP_SENDER_MAIN, 1001);
   		 pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Wieviele Rassenwechsel stehen mir zur Verfuegung?", GOSSIP_SENDER_MAIN, 1002);
        	pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Ich will meine Fraktion wechseln!", GOSSIP_SENDER_MAIN, 2001);
   		 pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Ich will eine Rundumanpassung!", GOSSIP_SENDER_MAIN, 2002);
   		 pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Ich will meine Rasse wechseln!", GOSSIP_SENDER_MAIN, 2003);
       	 

        	pPlayer->PlayerTalkClass->SendGossipMenu(907, pCreature->GetGUID());

        	return true;
    	}

    	bool OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 , uint32 uiAction)
    	{
        	pPlayer->PlayerTalkClass->ClearMenus();
       	 
        	switch (uiAction)
        	{
        	case 1000:
            	Options(pPlayer, pCreature, 10, 0, 0);
            	break;
        	case 1001:
            	Options(pPlayer, pCreature, 11, 0, 0);
            	break;
        	case 1002:
            	Options(pPlayer, pCreature, 12, 0, 0);
            	break;   			 
        	case 2001:
            	Options(pPlayer, pCreature,1,0,1);
            	break;
   		 case 2002:
            	Options(pPlayer, pCreature,2,0,1);
            	break;
   		 case 2003:
            	Options(pPlayer, pCreature,3,0,1);
            	break;
        	}


        	return true;
    	}

};

void AddSC_changer()
{
	new changer();
}
